﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Zxgl_downLoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            ArrayList LotteryArrayList = BLL.ArrayListClass.GetArrayList();
            try
            {
                FileStream file = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "zip_zxgl\\LotteryNoList.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(file);
               string[] strball = (string[])LotteryArrayList.ToArray(typeof(string));
                for (int i = 0; i < LotteryArrayList.Count; i++)
                {
                strball[i] = strball[i].Substring(0, strball[i].LastIndexOf(',')).Replace(",", " ") + strball[i].Substring(strball[i].LastIndexOf(',')).Replace(",", "+");
                sw.Write(strball[i].ToString()+"\r\n"); //开始写入   
                }
                sw.Flush(); //清空缓冲区     
                sw.Close();  //关闭文件       
                file.Close();
            }
            catch (IOException ex)
            {
                Response.Write("An IO exception has been thrown!");
                Response.Write(ex.ToString());
                return;
            }
            string userPath = AppDomain.CurrentDomain.BaseDirectory + @"zip_zxgl";
            dlZipDir(userPath, "cpmimi_ssq");
        
    }
    #region 下列代码为压缩并下载代码

    ZipOutputStream zos = null;

    String strBaseDir = "";

    void dlZipDir(string strPath, string strFileName)

    {

        MemoryStream ms = null;

        Response.ContentType = "application/octet-stream";

        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');

        Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".zip");

        ms = new MemoryStream();

        zos = new ZipOutputStream(ms);

        strBaseDir = strPath + "";

        addZipEntry(strBaseDir);

        zos.Finish();

        zos.Close();

        Response.Clear();

        Response.BinaryWrite(ms.ToArray());

        Response.End();

    }

    void addZipEntry(string PathStr)

    {

        DirectoryInfo di = new DirectoryInfo(PathStr);

        foreach (DirectoryInfo item in di.GetDirectories())
        {

            addZipEntry(item.FullName);

        }

        foreach (FileInfo item in di.GetFiles())
        {

            FileStream fs = File.OpenRead(item.FullName);

            byte[] buffer = new byte[fs.Length];

            fs.Read(buffer, 0, buffer.Length);

            string strEntryName = item.FullName.Replace(strBaseDir, "");

            ZipEntry entry = new ZipEntry(strEntryName);

            zos.PutNextEntry(entry);

            zos.Write(buffer, 0, buffer.Length);

            fs.Close();

        }

    }
    #endregion
}